<!--这是产品分类组件-->
<template>
	<div class="app-container">
    <div class="filter-container c-flex-row c-aligni-center c-justify-sb">
      <el-form class="form-container">
        <el-input @keyup.enter.native="handleFilter" v-model="listQuery.nameInput" clearable placeholder="搜索名称" style="width: 250px;"></el-input>
        <el-select clearable style="width: 120px;" v-model="listQuery.level" placeholder="分类级别">
          <el-option
              v-for="item in selectOptions"
              :key="item.id"
              :label="item.value"
              :value="item.id">
          </el-option>
        </el-select>
        <el-button class="search" type="primary" icon="el-icon-search" @click="handleFilter">搜索</el-button>
      </el-form>
			<div>
        <el-button v-if="permission == 3" type="primary" icon="el-icon-edit" @click="addCat(-1)">新增</el-button>
      </div>
		</div>
		<div>
			<el-table :data="tableData" row-key="id" v-loading="listLoading" element-loading-text="加载中..." :header-row-style="{'font-family': 'PingFangSC-Semibold','backgroundColor': '#f5f7fa','color':'#5c6065'}" fit highlight-current-row class="c-width100 color-222 family-PingFangSC-Regular font-s12">
			 	<el-table-column align="center" label="排序" width="170">
          <template slot-scope="scope">
            <span>{{scope.row.sort}}</span>
          </template>
        </el-table-column>
        <el-table-column  width="170"  align="center" label="一级分类">
          <template slot-scope="scope">
            <span v-if="scope.row.parent">{{scope.row.parent}}</span>
            <span v-else>{{scope.row.name}}</span>
          </template>
        </el-table-column>
        <el-table-column  width="170"  align="center" label="图片">
          <template slot-scope="scope">
            <img v-if="scope.row.avatar" :src="scope.row.avatar" width="80" height="80"/>
          </template>
        </el-table-column>
        <el-table-column  width="170"  align="center" label="二级分类">
          <template slot-scope="scope">
            <span v-if="scope.row.parent">{{scope.row.name}}</span>
          </template>
        </el-table-column>
        <el-table-column  align="center" label="操作">
          <template slot-scope="scope">
            <b v-if="permission == 3" @click="deleteSort(scope.row.gcId, scope.row.parentId)">删除</b>
            <span class="color-409" v-if="permission == 3">|</span>
            <b v-if="permission == 3" @click="addCat(scope.$index)">编辑</b>
            <span class="color-409" v-if="permission == 3">|</span>
            <b @click="linkToProduct(scope.row.gcId)">查看产品</b>
          </template>
        </el-table-column>
		   </el-table>
		</div>
    <div class="block text-c">
      <!--分页组件 current-page:当前页   -->
      <!--   page-sizes:每页几条数据（选项）   -->
      <!--   page-size:每页几条数据   -->
      <!--   total:总条数   -->
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="listQuery.page"
          :page-sizes="[10, 20, 30, 50]"
          :page-size="10"
          layout="total, sizes, prev, pager, next, jumper"
          :total="listQuery.total">
        </el-pagination>
    </div>
		<addProductSort v-if="isShowAdd" :catData="catData" @hideAdd="hideAdd" @saveCat="saveCat"></addProductSort>
		<!-- <editSortPic v-show="isShowEdit" @hideEdit="hideEdit"></editSortPic> -->
	</div>
</template>
<script>
import { getCatList, deleteCat } from '@/api/goods'
import addProductSort from '../components/addProductSort.vue'
import clip from '@/utils/clipboard'
import editSortPic from '../components/editSortPic.vue'
import { mall_mallInfo } from '@/utils/permission'
export default {
  components: {
    addProductSort,
    editSortPic
  },
  data() {
    return {
      permission: 3,
      listQuery: {
        currentPage: 1, // 当前页
        total: null, // 数据总条数
        nameInput: null,
        level: null,
        page: 1
      },
      isShowAdd: false, // 是否显示增加分类组件
      isShowEdit: false,
      listLoading: false,
      tableData: [],
      catData: [],
      selectOptions: [ // 用户身份下拉菜单选择项
        {
          id: 1,
          value: '一级分类'
        }, {
          id: 2,
          value: '二级分类'
        }
      ]
    }
  },
  created() {
    this.getModelPermission()
    this.getList()
  },
  methods: {
    getModelPermission() {
      this.permission = mall_mallInfo()
    },
    getList() {
      this.listLoading = true
      getCatList(this.listQuery).then(res => {
        this.tableData = res.data.data.list.data
        this.listQuery.total = res.data.data.list.total
        this.listLoading = false
      }).catch(err => {
        console.log(err)
      })
    },
    addCat(index) {
      if (index == -1) {
        this.catData = { type: '1', gcId: null, parentId: 0, sort: 0, avatar: null, name: null }
      } else {
        this.catData = {
          type: this.tableData[index].parentId == 0 ? '1' : '2',
          gcId: this.tableData[index].gcId,
          parentId: this.tableData[index].parentId,
          sort: this.tableData[index].sort,
          avatar: this.tableData[index].avatar,
          name: this.tableData[index].name
          // fileList: this.tableData[index].avatar ? [{ url: this.tableData[index].avatar }] : []
        }
      }
      this.isShowAdd = true
    },
    hideAdd(msg) {
      this.isShowAdd = msg
    },
    saveCat() {
      this.getList()
    },
    hideEdit(msg) {
      this.isShowEdit = msg
    },
    deleteSort(gcId, parentId) {
      this.$confirm('确定删除？' + (parentId == 0 ? '删除后，该分类的子分类也会删除。' : ''), '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        var data = { gcId: gcId }
        deleteCat(data).then(res => {
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
          this.getList()
        }).catch(err => {
          console.log(err)
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    handleSizeChange(val) { // 切换每页几条数据方法
      // console.log(`每页 ${val} 条`)
      this.listQuery.limit = val
      this.getList()
    },
    handleCurrentChange(val) { // 切换当前页
      // console.log(`当前页: ${val}`)
      this.listQuery.page = val
      this.getList()
    },
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
    linkToProduct(sortName) {
      this.$router.push({ path: `/mall/product/${sortName}` })
    }
  }
}
</script>

<style scoped>

</style>
